package com.qingke.Qiximybatis.business;

import org.apache.ibatis.session.SqlSession;
import com.qingke.Qiximybatis.QixiApp;
import com.qingke.Qiximybatis.QixiConsole;
import com.qingke.Qiximybatis.QixiDao;
import com.qingke.Qiximybatis.command.UserCommand;
import com.qingke.Qiximybatis.mapper.MyUserMapper;
import com.qingke.Qiximybatis.sqlclass.MyUser;

public class Update3 extends UserCommand{
	
	public Update3(MyUser user) {
		super(user);
	}

	@Override
	public void executeUserCommand() {
		SqlSession session = QixiDao.mybatisSession();
		System.out.println("欢迎进入修改页面：");
		MyUser user = QixiApp.getInstance().getUser();
		System.out.println(user);

		//判断姓名输入
		String name = QixiConsole.nullUserInput("请输入要修改的name（无则直接跳过）:");
		if(!"".equalsIgnoreCase(name)){
			user.setName(name);
		}
		//判断身高输入
		String height = QixiConsole.nullUserInput("请输入要修改的height（无则直接跳过）：");
		while(true){
			if(height.equalsIgnoreCase("")){
				break;
			}
			int iheight ;
			try {
				iheight= Integer.parseInt(height);
				break;
			} catch (Exception e) {
				height = QixiConsole.nullUserInput("输入的内容不正确请重新输入！");
			}
		}
		if(!"".equalsIgnoreCase(height)){
			user.setHeight(height);
		}
		//判断年龄输入
		String age = QixiConsole.nullUserInput("请输入要修改的age（无则直接跳过）：");
		while(true){
			if(age.equalsIgnoreCase("")){
				break;
			}
			int iage ;
			try {
				iage= Integer.parseInt(age);
				break;
			} catch (Exception e) {
				age = QixiConsole.nullUserInput("输入的内容不正确请重新输入！");
			}
		}
		if(!"".equalsIgnoreCase(age)){
			user.setAge(age);
		}
		//判断密码输入
		String password = QixiConsole.nullUserInput("请输入要修改的password（无则直接跳过）：");
		if(!"".equalsIgnoreCase(password)){
			user.setUsername(password);			
		}
		//判断性别输入
		String gender = QixiConsole.nullUserInput("请输入要修改的gender（无则直接跳过）：");	
		while(true){
			if(gender.equalsIgnoreCase("F")&&gender.equalsIgnoreCase("M")){
				System.out.println("输入的内容不正确请重新输入！");
			}else{
				break;
			}
		}
		if(!"".equalsIgnoreCase(gender)){
			user.setGender(gender);
		}
		
		MyUserMapper usermapper = session.getMapper(MyUserMapper.class);
		usermapper.updateUser(user);
		session.commit();
		System.out.println("修改成功了！");	
		session.close();
		
	}
}
